cmake_minimum_required(VERSION 2.6)
if(COMMAND CMAKE_POLICY)
  cmake_policy(SET CMP0003 NEW)
endif(COMMAND CMAKE_POLICY)

project(cg_examples)

set(CMAKE_MODULE_PATH "${cg_examples_SOURCE_DIR}/cmake")
set(CMAKE_INSTALL_PREFIX "${cg_examples_BINARY_DIR}/dist" CACHE PATH "cg_examples install prefix" FORCE)

if(WIN32)
#option(GLUT_GLEW_FREEIMAGE_IN_WINSDK "Header and library files of glut, glew and FreeImage are placed in windows sdk dir." FALSE)
find_package(WindowsSDK QUIET)
if(WINSDK_FOUND)
  find_path(GLUT_INCLUDE_DIR NAMES GL/glut.h PATHS ${WINSDK_INCLUDE_DIR})
  find_library(GLUT_glut_LIBRARY NAMES glut glut32 freeglut PATHS ${WINSDK_LIBRARY_DIR})
  find_path(GLEW_INCLUDE_DIR NAMES GL/glew.h PATHS ${WINSDK_INCLUDE_DIR})
  find_library(GLEW_GLEW_LIBRARY NAMES glew32 PATHS ${WINSDK_LIBRARY_DIR})
  find_path(FREEIMAGE_INCLUDE_DIR NAMES FreeImage.h PATHS ${WINSDK_INCLUDE_DIR})
  find_library(FREEIMAGE_FreeImage_LIBRARY NAMES FreeImage PATHS ${WINSDK_LIBRARY_DIR})
endif(WINSDK_FOUND)
endif(WIN32)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(GLEW QUIET)
find_package(FreeImage QUIET)
find_package(MITK QUIET)

#if(NOT GLUT_FOUND)
#  message(FATAL_ERROR "${PROJECT_NAME} requires GLUT.")
#endif(NOT GLUT_FOUND)

include_directories(
  "include"
  ${OPENGL_INCLUDE_DIR}
  ${GLUT_INCLUDE_DIR}
)

add_custom_target(copy_data ${CMAKE_COMMAND} -E copy_directory ${cg_examples_SOURCE_DIR}/data ${cg_examples_BINARY_DIR}/data)

install(DIRECTORY data/ DESTINATION data PATTERN ".svn" EXCLUDE)
install(DIRECTORY src/shaders/ DESTINATION shaders PATTERN ".svn" EXCLUDE)

function(add_example EXAMPLE_NAME)
  set(LIBRARIES ${GLUT_glut_LIBRARY} ${OPENGL_LIBRARIES})
  if(ARGV1)
    set(ADDITIONAL_LIBRARYS ${ARGV})
	list(REMOVE_AT ADDITIONAL_LIBRARYS 0)
	list(APPEND LIBRARIES ${ADDITIONAL_LIBRARYS})
  endif(ARGV1)
  add_executable(${EXAMPLE_NAME} src/${EXAMPLE_NAME}.cpp)
  target_link_libraries(${EXAMPLE_NAME} ${LIBRARIES})
  install(TARGETS ${EXAMPLE_NAME} RUNTIME DESTINATION . CONFIGURATIONS Release)  
endfunction(add_example)

add_example(sierpinski)
add_example(picksquare)
add_example(transform)
add_example(sphere)
add_example(curves)
add_example(nurbscurve)
add_example(surface)
add_example(gravity)
add_example(gravity2)
add_example(mandelsimp)

if(FREEIMAGE_FOUND)
  include_directories(${FREEIMAGE_INCLUDE_DIR})
  add_example(texture2d ${FREEIMAGE_LIBRARIES})
  add_dependencies(texture2d copy_data)
  add_example(mirror ${FREEIMAGE_LIBRARIES})
  add_dependencies(mirror copy_data)
  add_example(planet ${FREEIMAGE_LIBRARIES})
  add_dependencies(planet copy_data)
endif(FREEIMAGE_FOUND)

if(MITK_FOUND)
  include_directories(${MITK_INCLUDE_DIR})
  add_example(spheremap ${MITK_LIBRARIES})
  add_dependencies(spheremap copy_data)
  add_example(dyncubemap ${MITK_LIBRARIES})
  add_dependencies(dyncubemap copy_data)
endif(MITK_FOUND)

if(GLEW_FOUND)
  include_directories(${GLEW_INCLUDE_DIR})
  configure_file(src/shaders/phong.vert ${cg_examples_BINARY_DIR}/shaders/phong.vert COPYONLY)
  configure_file(src/shaders/phong.frag ${cg_examples_BINARY_DIR}/shaders/phong.frag COPYONLY)
  add_example(phongshader ${GLEW_GLEW_LIBRARY})
endif(GLEW_FOUND)

